//
//  TalkShareSelectView.m
//  talkDemo
//
//  Created by telen on 15/11/9.
//  Copyright © 2015年 刘赞黄Telen. All rights reserved.
//

#import "TalkShareSelectView.h"
#import "TalkDefine.h"
#import "DataProcessing.h"
#define white_share_bg 0.01
#define white_share_bg_alpha 0.5

@implementation TalkShareSelectView
{
    UIView* kuang;
    UIButton* wxBtn;
    UIButton* qqBtn;
}

+ (instancetype)talkShareView_ratio:(CGFloat)ratio_w inSuperView:(UIView *)superView
{
    TalkShareSelectView* instance = [[self alloc] initWithFrame:CGRectMake(0, 0, 600/ratio_w, 350/ratio_w) ratio:ratio_w inSuperView:superView];
    [superView addSubview:instance];
    [instance show];
    return instance;
}

- (instancetype)initWithFrame:(CGRect)frame ratio:(CGFloat)ratio_w inSuperView:(UIView *)superView
{
    CGRect frame_cover = superView.bounds;
    if (self = [super initWithFrame:frame_cover]) {
        //bgColor
        self.backgroundColor = [UIColor colorWithWhite:white_share_bg alpha:white_share_bg_alpha];
        //kuang
        kuang = [[UIView alloc] initWithFrame:frame];
        kuang.center = CGPointMake(frame_cover.size.width/2, frame_cover.size.height/2);
        [self addSubview:kuang];
        //bg
        frame.origin = CGPointZero;
        CGSize size = frame.size;
        UIImageView* bgv = [[UIImageView alloc] initWithFrame:frame];
        UIImage* bg = [UIImage imageNamed:@"img_share_bg.png"];
        bg = [bg stretchableImageWithLeftCapWidth:bg.size.width*0.5 topCapHeight:bg.size.height*0.5];
        bgv.image = bg;
        [kuang addSubview:bgv];
        //
        UILabel* title = [[UILabel alloc] initWithFrame:CGRectMake(20/ratio_w, 0, size.width-40/ratio_w, 80/ratio_w)];
        title.text = TalkLocalizedString(@"shareTo", "");
        title.font = [UIFont boldSystemFontOfSize:32/ratio_w];
        title.textAlignment = NSTextAlignmentCenter;
        [bgv addSubview:title];
        //
        size = CGSizeMake(89/ratio_w, 89/ratio_w);
        UIButton* fbbtn = [self getInstanceButtonforNormal:@"talk_btn_facebook_normal.png" down:@"talk_btn_facebook_down.png" size:size  ratio:ratio_w];
        fbbtn.center = CGPointMake(100/ratio_w, 180/ratio_w);
        [kuang addSubview:fbbtn];
        //
        UIButton* wbbtn = [self getInstanceButtonforNormal:@"talk_btn_sina_normal.png" down:@"talk_btn_sina_down.png" size:size ratio:ratio_w];
        wbbtn.center = CGPointMake(240/ratio_w, 180/ratio_w);
        [kuang addSubview:wbbtn];
        //
        UIButton* qqbtn = [self getInstanceButtonforNormal:@"talk_btn_qq_normal.png" down:@"talk_btn_qq_down.png" size:size ratio:ratio_w];
        qqbtn.center = CGPointMake(370/ratio_w, 180/ratio_w);
        [kuang addSubview:qqbtn];
        qqBtn = qqbtn;
        //
        UIButton* wxbtn = [self getInstanceButtonforNormal:@"talk_btn_wechat_normal.png" down:@"talk_btn_wechat_down.png" size:size ratio:ratio_w];
        wxbtn.center = CGPointMake(500/ratio_w, 180/ratio_w);
        [kuang addSubview:wxbtn];
        wxBtn = wxbtn;
        //
//        [self addPoint:[self center_with:fbbtn and:wbbtn] ratio:ratio_w];
//        [self addPoint:[self center_with:qqbtn and:wbbtn] ratio:ratio_w];
//        [self addPoint:[self center_with:qqbtn and:wxbtn] ratio:ratio_w];
        //
        size = CGSizeMake(50/ratio_w, 50/ratio_w);
        UIButton* xbtn = [self getInstanceButtonforNormal:@"talk_btn_close_normal.png" down:@"talk_btn_close_down.png" size:size ratio:ratio_w];
        xbtn.center = CGPointMake(frame.size.width-size.width*0.8,size.height*0.8);
        [kuang addSubview:xbtn];
        //
        [fbbtn addTarget:self action:@selector(fb:) forControlEvents:UIControlEventTouchUpInside];
        [wbbtn addTarget:self action:@selector(weibo:) forControlEvents:UIControlEventTouchUpInside];
        [qqbtn addTarget:self action:@selector(qq:) forControlEvents:UIControlEventTouchUpInside];
        [wxbtn addTarget:self action:@selector(wechat:) forControlEvents:UIControlEventTouchUpInside];
        [xbtn addTarget:self action:@selector(clicked_X:) forControlEvents:UIControlEventTouchUpInside];
        //
        //fbbtn.enabled = NO;
        //fbbtn.transform = CGAffineTransformMakeScale(0.75, 0.75);
    }
    return self;
}

- (UIButton*)getInstanceButtonforNormal:(NSString*)normal down:(NSString*)down size:(CGSize)size ratio:(CGFloat)ratio_w
{
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, size.width, size.height);
    [btn setBackgroundImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:down] forState:UIControlStateHighlighted];
    return btn;
}

- (void)addPoint:(CGPoint)point ratio:(CGFloat)ratio_w
{
    UIImageView* imgv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 12, 12)];
    imgv.image = [UIImage imageNamed:@"talk_point.png"];
    imgv.center = point;
    [kuang addSubview:imgv];
}

- (CGPoint)center_with:(UIView*)v1 and:(UIView*)v2
{
    return CGPointMake((v1.center.x+v2.center.x)/2, (v1.center.y+v2.center.y)/2);
}

- (void)show
{
    self.backgroundColor = [UIColor colorWithWhite:white_share_bg alpha:0];
    kuang.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
    [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.3 animations:^{
            kuang.hidden = NO;
            kuang.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            self.backgroundColor = [UIColor colorWithWhite:white_share_bg alpha:white_share_bg_alpha];
        }];
        [UIView addKeyframeWithRelativeStartTime:0.7 relativeDuration:0.3 animations:^{
            kuang.transform = CGAffineTransformMakeScale(1, 1);
        }];
    } completion:^(BOOL finished) {
        kuang.hidden = NO;
        self.backgroundColor = [UIColor colorWithWhite:white_share_bg alpha:white_share_bg_alpha];
    }];
}

- (void)unshow
{
    self.backgroundColor = [UIColor colorWithWhite:white_share_bg alpha:white_share_bg_alpha];
    [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
            kuang.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            self.backgroundColor = [UIColor colorWithWhite:white_share_bg alpha:0];
        }];
        [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.2 animations:^{
            kuang.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
        }];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)setDelegate:(id<TalkVC_Delegate>)delegate{
    _delegate = delegate;
    [self checkShareEnable_Controller];
}

- (void)checkShareEnable_Controller
{
    if (_delegate && [_delegate respondsToSelector:@selector(talkViewShareEnable__ShareTo:)]) {
        BOOL ret = [_delegate talkViewShareEnable__ShareTo:TalkShareTo_qq];
        if(ret){
            qqBtn.hidden = NO;
        }else{
            CGFloat ratio_w = [DataProcessing shareSingle].ratio_w;
            qqBtn.hidden = YES;
            wxBtn.center = CGPointMake(370 / ratio_w, 180 / ratio_w);
        }
        ret = [_delegate talkViewShareEnable__ShareTo:TalkShareTo_wechat];
        if(ret){
            wxBtn.hidden = NO;
        }else{
            wxBtn.hidden = YES;
        }
    }
}

#pragma mark- Event
- (void)clicked_X:(id)sender
{
    [self unshow];
}

- (void)fb:(id)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(talkViewDidClicked_ShareTo:withInfo:)]) {
        [_delegate talkViewDidClicked_ShareTo:TalkShareTo_faceBook withInfo:_shareDict];
    }
    [self unshow];
}

- (void)weibo:(id)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(talkViewDidClicked_ShareTo:withInfo:)]) {
        [_delegate talkViewDidClicked_ShareTo:TalkShareTo_weibo withInfo:_shareDict];
    }
    [self unshow];
}

- (void)qq:(id)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(talkViewDidClicked_ShareTo:withInfo:)]) {
        [_delegate talkViewDidClicked_ShareTo:TalkShareTo_qq withInfo:_shareDict];
    }
    [self unshow];
}

- (void)wechat:(id)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(talkViewDidClicked_ShareTo:withInfo:)]) {
        [_delegate talkViewDidClicked_ShareTo:TalkShareTo_wechat withInfo:_shareDict];
    }
    [self unshow];
}

@end
